package com;


import cn.hutool.core.date.StopWatch;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 比较 ArrayList LinkedList 尾插速度。
 * 在数据多的的时候 LinkedList 尾插特别慢！
 * 头插入 LinkedList 是最快的
 *
 * @author yunlongn
 */
public class Test01 {

    public static void main(String[] args) throws Exception {
        int count = 50_0000;
        test1(count);
        test2(count);
    }

    /**
     * 头插
     */
    private static void test1(int count) throws Exception {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("ArrayList");
        Object o = new Object();
        List<Object> list1 = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            list1.add(0, o);
        }
        stopWatch.stop();
        stopWatch.start("LinkedList");
        List<Object> list2 = new LinkedList<>();
        for (int i = 0; i < count; i++) {
            list2.add(0, o);
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
        System.out.println(list2.size());
        System.out.println(list1.size());
    }

    /**
     * 尾插
     */
    private static void test2(int count) throws Exception {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("ArrayList");
        Object o = new Object();
        List<Object> list1 = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            list1.add(o);
        }
        stopWatch.stop();
        stopWatch.start("LinkedList");
        List<Object> list2 = new LinkedList<>();
        for (int i = 0; i < count; i++) {
            list2.add(o);
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
        System.out.println(list2.size());
        System.out.println(list1.size());
    }
}

